<?php

namespace app\common\services;

use app\api\model\wanlshop\Order;
use app\api\model\wanlshop\OrderGoods;
use app\common\model\User;
use PDOStatement;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class UserServices
{
    /**
     * @param string $user_id
     * @return bool|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function get_user_spread(string $user_id = ''){
        $data =  (new User())->where('spread_id',$user_id)->order('id desc')->select();
        $vip_levels_str = config('site.user_level_vips');
        foreach ($data as &$value){
            $value['vip_level_str'] = $vip_levels_str['user_vip_'.$value['buy_vip_level']];
            //总消费金额
            $value['xiaofei_moneys'] = 0;
            //今日金额
            
            $value['xiaofei_moneys_today'] = 0;
            //昨日消费
            $value['xiaofei_moneys_yestoday'] = 0;

            $order_id = (new Order())->where('state','>',1)->where('user_id',$value['id'])->column('id');
            //消费金额
            if(!empty($order_id)){
                $value['xiaofei_moneys'] = (new OrderGoods())->whereIn('order_id',$order_id)->sum('actual_payment');

                $value['xiaofei_moneys_today'] = (new OrderGoods())->whereIn('order_id',$order_id)->where('createtime','>=',strtotime(date('Y-m-d')))->sum('actual_payment');
                $value['xiaofei_moneys_yestoday'] = (new OrderGoods())->whereIn('order_id',$order_id)
                    ->where('createtime','<',strtotime(date('Y-m-d')))
                    ->where('createtime','<',strtotime(date('Y-m-d'))-86400)
                    ->sum('actual_payment');
            }
        }
        return  $data;
    }
}